package ro.fortech.workshop_demo.business;

import android.content.Intent;
import ro.fortech.workshop_demo.access.webservice.apis.WSManagerSeasonsApi;
import ro.fortech.workshop_demo.access.webservice.callbacks.GrandPrixParserDelegate;
import ro.fortech.workshop_demo.data.GrandPrix;
import ro.fortech.workshop_demo.utils.Utils;

import java.util.ArrayList;

/**
 * User: florinv
 * Date: 4/16/13
 * Time: 11:44 AM
 */
public class SeasonsManager implements GrandPrixParserDelegate {
    /**
     * Singleton instance
     */
    private static SeasonsManager instance;
    /**
     * Array holding the parsed reaces
     */
    private ArrayList<GrandPrix> grandPrixList;

    /**
     * Private constructor
     */
    private SeasonsManager() {
        grandPrixList = new ArrayList<GrandPrix>();
    }

    /**
     * Singleton instance retrieval method
     */
    public static SeasonsManager getInstance() {
        if (instance == null) {
            instance = new SeasonsManager();
        }
        return instance;
    }

    /**
     * Method used for calling the api method for retrieving the grand prix list for the selected season
     */
    public void getGrandPrixList(String season) {
        // Call the method from the api manager
        WSManagerSeasonsApi.getInstance().getGrandPrixList(season, this);
    }

    /**
     * Callback method used for handling the get grand prix list response
     */
    @Override
    public void getSeasonsCallback(ArrayList<GrandPrix> grandPrixList, int responseCode) {
        // Handle according to the response code
        if (responseCode == 200) {
            // Success
            // Store the list in the manager
            this.grandPrixList.clear();
            this.grandPrixList.addAll(grandPrixList);
            // Send success notification to the UI
            Utils.getApplicationContext().sendBroadcast(new Intent(BusinessConstants.NTF_SM_GRAND_PRIX_LIST_PARSE_SUCCESS));
        } else {
            // Error
            // Send error notification to the UI
            Utils.getApplicationContext().sendBroadcast(new Intent(BusinessConstants.NTF_SM_GRAND_PRIX_LIST_PARSE_ERROR));
        }
    }

    public ArrayList<GrandPrix> getGrandPrixList() {
        return grandPrixList;
    }
}
